<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <!--<el-form-item label="排产状态" prop="mplanStatus">
        <el-select v-model="queryParams.mplanStatus" placeholder="请选择" clearable>
          <el-option v-for="dict in planStatusList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>-->
      <el-form-item label="产地" prop="factoryId">
        <el-select v-model="queryParams.factoryId" placeholder="请选择" filterable clearable @change="factoryChange">
          <el-option v-for="team in factoryList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="班组" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择" filterable clearable>
          <el-option v-for="team in teamList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="月份" prop="month">
        <el-select v-model="queryParams.month" placeholder="请选择" clearable>
          <el-option
            v-for="dict in monthList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="teamSelect" @click="handleExport"
          v-hasPermi="['factory:factorymplan:edit']">导出月计划物料</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamMplanBomList" @selection-change="handleSelectionChange">
      <el-table-column label="物料编码" align="center" prop="bomCode" />
      <el-table-column label="物料名称" align="center" prop="bomName" />
      <el-table-column label="规格型号" align="center" prop="bomStandard" />
      <el-table-column label="版本" align="center" prop="bomVersion" />
      <el-table-column label="材料单位" align="center" prop="bomUnit" />
      <el-table-column label="分子" align="center" prop="numerator" />
      <el-table-column label="对应产品" align="center" prop="productName" />
      <el-table-column label="订单量" align="center" prop="orderNum" />
      <el-table-column label="损耗" align="center" prop="loseNum" />
      <el-table-column label="安全库存" align="center" prop="secStoreNum" />
      <el-table-column label="即时库存" align="center" prop="storNum" />
      <el-table-column label="物料需求" align="center" prop="needNum" width="100"/>
    </el-table>

   <!--<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />--> 
  </div>
</template>

<script>
import { printMplanBom, teamMplanBomList } from "@/api/sm/factory/factorymplan";
import { listTeam,listFactory } from "@/api/system/dept";

export default {
  name: "Orderinfo",
  dicts: ['order_status'],
  data() {
    return {
      // 遮罩层
      loading: false,
      calShow: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      teamSelect: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      plantotal: 0,
      //分厂待排产订单信息表格数据
      teamMplanBomList: [],
      //排产工单
      planWorkOrderList: [],
      //排产结果
      planResultList: [],
      factoryList: [],
      teamList: [],
      monthList: [
        {
          "label": "2024年1月",
          "value": '2024-01'
        },
        {
          "label": "2024年2月",
          "value": '2024-02'
        },
        {
          "label": "2024年3月",
          "value": '2024-03'
        },
        {
          "label": "2024年4月",
          "value": '2024-04'
        },
        {
          "label": "2024年5月",
          "value": '2024-05'
        },
        {
          "label": "2024年6月",
          "value": '2024-06'
        },
        {
          "label": "2024年7月",
          "value": '2024-07'
        },
        {
          "label": "2024年8月",
          "value": '2024-08'
        },
        {
          "label": "2024年9月",
          "value": '2024-09'
        },
        {
          "label": "2024年10月",
          "value": '2024-10'
        },
        {
          "label": "2024年11月",
          "value": '2024-11'
        },
        {
          "label": "2024年12月",
          "value": '2024-12'
        }
      ],
      planStatusList: [
        {
          "label": "待排产",
          "value": 0
        }
        ,
        {
          "label": "排产完成",
          "value": 1
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 同步时间时间范围
      daterangeOrderDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamId: null,
        bomCode: null,
        bomName: null,
        mplanStatus: 0,
        workerConfirmstatus: 1,
        workerOrdertype: 1
      },
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getFactoryList();
  },
  methods: {
    /** 查询订单信息列表 */
    getFactoryList() {
      this.loading = true;
      listFactory({"level":2}).then(response => {
        this.factoryList = response.data;
        this.queryParams.factoryId = this.factoryList[0].deptId;
        this.getTeamList(this.queryParams.factoryId);
        this.loading = false;
      });
    },
    factoryChange(){
      this.getTeamList(this.queryParams.factoryId);
    },
    /** 查询订单信息列表 */
    getTeamList(parentId) {
      this.loading = true;
      listTeam({"parentId":parentId}).then(response => {
        this.teamList = response.data;
        this.loading = false;
      });
    },

    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      teamMplanBomList(this.queryParams.month,this.queryParams.teamId).then(response => {
        this.teamMplanBomList = response.data;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if(this.queryParams.teamId!=null && this.queryParams.teamId!=undefined){
        this.teamSelect = false;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleExport(row) {
      this.loading = true;
      printMplanBom(this.queryParams.month,this.queryParams.teamId).then(response => {
        let blob = new Blob([response], { type: "application/vnd.ms-excel" });
        var downloadElement = document.createElement("a");

        var href = window.URL.createObjectURL(blob); //创建下载的链接

        downloadElement.href = href;

        downloadElement.download = '月排产_物料计划'; //下载后文件名

        document.body.appendChild(downloadElement);

        downloadElement.click(); //点击下载

        document.body.removeChild(downloadElement); //下载完成移除元素

        window.URL.revokeObjectURL(href); //释放掉blob对象

        this.loading = false;
      });
    }
  }
};
</script>
